﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lswweb.Model;
using Lswweb.IDAL;

namespace Lswweb.SqlServerDAL
{
    public class MessagesDAL : IMessages
    {
        #region 成员字段及构造方法

        private Builder<MessageInfo> builder = new Builder<MessageInfo>("Messages", "Id");

        public MessagesDAL()
        {
            builder.CreateModel += new ReaderBind<MessageInfo>(CreateModel);
            builder.GetParameters += new ParametersBuilder<MessageInfo>(GetParameters);
        }

        IDataParameter[] GetParameters(MessageInfo t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@Content",string.IsNullOrEmpty(t.Content)?null:t.Content),
                DataAccess.CreateParameter("@Email",string.IsNullOrEmpty(t.Email)?null:t.Email),
                DataAccess.CreateParameter("@Fax",string.IsNullOrEmpty(t.Fax)?null:t.Fax),
                DataAccess.CreateParameter("@LinkMan",string.IsNullOrEmpty(t.LinkMan)?null:t.LinkMan),
                DataAccess.CreateParameter("@Mobile",string.IsNullOrEmpty(t.Mobile)?null:t.Mobile),
                DataAccess.CreateParameter("@Phone",string.IsNullOrEmpty(t.Phone)?null:t.Phone),
                DataAccess.CreateParameter("@FormUserId",t.FormUserId),
                DataAccess.CreateParameter("@ToUserId",t.ToUserId),
                DataAccess.CreateParameter("@Id",t.Id),
                DataAccess.CreateParameter("@IsRead",t.IsRead)};
        }

        MessageInfo CreateModel(Hashtable reader)
        {
            MessageInfo guestBook = new MessageInfo();

            if (reader.ContainsKey("Content"))
                guestBook.Content = reader["Content"].ToString();

            if (reader.ContainsKey("Email"))
                guestBook.Email = reader["Email"].ToString();

            if (reader.ContainsKey("Fax"))
                guestBook.Fax = reader["Fax"].ToString();

            if (reader.ContainsKey("Id"))
                guestBook.Id = Convert.ToInt32(reader["Id"]);

            if (reader.ContainsKey("IsRead"))
                guestBook.IsRead = Convert.ToBoolean(reader["IsRead"]);

            if (reader.ContainsKey("LinkMan"))
                guestBook.LinkMan = reader["LinkMan"].ToString();

            if (reader.ContainsKey("Mobile"))
                guestBook.Mobile = reader["Mobile"].ToString();

            if (reader.ContainsKey("Phone"))
                guestBook.Phone = reader["Phone"].ToString();

            if (reader.ContainsKey("FormUserId"))
                guestBook.FormUserId = Convert.ToInt32(reader["FormUserId"]);

            if (reader.ContainsKey("ToUserId"))
                guestBook.ToUserId = Convert.ToInt32(reader["ToUserId"]);

            if (reader.ContainsKey("DateCreated"))
                guestBook.DateCreated = Convert.ToDateTime(reader["DateCreated"]);

            return guestBook;
        }

        #endregion

        #region MessagesDAL 成员

        public MessageInfo GetModel(int id)
        {
            if (id <= 0)
                return null;

            return builder.GetModel(id);
        }

        public IList<MessageInfo> GetModels(bool isRead, int pageSize, int pageIndex, out int totalRecord)
        {
            string where = "IsRead=@IsRead";

            return builder.GetModels(string.Empty, where, "id desc", pageIndex, pageSize, out totalRecord, DataAccess.CreateParameter("IsRead",isRead));
        }

        public int Insert(MessageInfo model)
        {
            if (model == null)
                return -1;

            return builder.Insert(
                model,
                "FormUserId",
                "ToUserId",
                "LinkMan",
                "Phone",
                "Mobile",
                "Fax",
                "Email",
                "Content");
        }

        public bool Delete(int id)
        {
            if (id <= 0)
                return false;

            return builder.Delete(id);
        }

        public bool UpdateIsRead(int id)
        {
            if (id <= 0)
                return false;

            return builder.UpdateFields("IsRead",id,true);
        }

        #endregion
    }
}
